package com.jdz.gxq.info.admin.smallloan.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import jp.terasoluna.fw.dao.QueryDAO;
import jp.terasoluna.fw.dao.UpdateDAO;

import com.jdz.gxq.info.admin.smallloan.bo.Smallloan;

public class SmallloanServiceImpl implements SmallloanService {

	private QueryDAO queryDAO = null;

	private UpdateDAO updateDAO = null;

	/**
	 * Smallloan检索分页用，查询记录条数
	 * 
	 * @return
	 */
	public int getSmallloansCount(String enterpriseName) {

		int totalCount = queryDAO.executeForObject(
				"smallloan.getSmallloansCount", enterpriseName, Integer.class);

		return totalCount;
	}

	/**
	 * Smallloan检索
	 * 
	 * @return
	 */
	public List<Smallloan> getSmallloans(Map<String, Object> params) {

		return queryDAO.executeForObjectList("smallloan.getSmallloans", params);
	}

	/**
	 * 追加Smallloan
	 * 
	 * @param smallloan
	 */
	public void insertSmallloan(Smallloan smallloan) {

		updateDAO.execute("smallloan.insertSmallloan", smallloan);
	}

	/**
	 * 更新Smallloan
	 * 
	 * @param smallloan
	 */
	public void updateSmallloan(Smallloan smallloan) {

		updateDAO.execute("smallloan.updateSmallloan", smallloan);
	}

	/**
	 * 通过id得到smallloan
	 * 
	 * @param id
	 * @return
	 */
	public Smallloan getSmallloanById(int id) {

		Smallloan smallloan = queryDAO.executeForObject(
				"smallloan.getSmallloanById", id, Smallloan.class);

		return smallloan;
	}

	/**
	 * 删除Smallloan
	 * 
	 * @param smallloan
	 */
	public void deleteSmallloan(int id) {

		updateDAO.execute("smallloan.deleteSmallloanById", id);
	}

	/**
	 * 验证smallloanName是否重复 flag: 1 追加时验证 flag: 2 更新时验证
	 */
	public boolean checkSmallloanName(Smallloan paramSmallloan, int flag) {

		Map<String, Object> params = new HashMap<String, Object>();
		params.put("lendTime", paramSmallloan.getLendTime());
		params.put("recipientName", paramSmallloan.getRecipientName());

		Smallloan smallloan = queryDAO.executeForObject(
				"smallloan.getSmallloanByNameAndTime", params, Smallloan.class);

		if (smallloan == null) {
			return true;
		}

		// 更新验证，查出来的数据是不是本身
		if (flag == 2) {

			if (smallloan.getId() == paramSmallloan.getId()) {
				return true;
			}
		}

		return false;
	}

	public void setQueryDAO(QueryDAO queryDAO) {
		this.queryDAO = queryDAO;
	}

	public void setUpdateDAO(UpdateDAO updateDAO) {
		this.updateDAO = updateDAO;
	}

}
